package com.example.projectjava.service;


import com.example.projectjava.dao.CategoryDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CategoryService {

    @Autowired
    CategoryDao categoryDao;//spring会自动将userDao注入到此类为(自动为该属性赋值)

    public  int insertCategory(String category_id,String category_name,String product_count){
        return categoryDao.insertCategory(category_id,category_name,product_count);
    }

    public int deleteCategory(String category_id) {
        int res = categoryDao.deleteCategory(category_id);
        if (res <= 0) {
            throw new RuntimeException("该分类不存在");
        } else {
            return 1;
        }
    }

    public List<Map<String, Object>> queryAllCategory() {
        return categoryDao.queryAllCategory();
    }

    public int updateCategory(String category_id, String category_name, String product_count) {
        int res = categoryDao.updateCategory(category_id, category_name, product_count);
        if (res <= 0) {
            throw new RuntimeException("该分类不存在");
        }
        return 1;
    }

    public List<Map<String, Object>> queryProductByCategory(String category_id) {
        return categoryDao.queryProductByCategory(category_id);
    }
}

